<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Shop\Brand;

class BrandController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //查找
        $where = function ($query) use ($request) {
            if ($request->has('keyword') and $request->keyword != '') {
                $search = "%" . $request->keyword . "%";
                $query->where('name', 'like', $search);
            }
        };
        $brands = Brand::where($where)->orderBy('sort_order')->paginate(10);
        return view('admin.shop.brands.index')->with('brands', $brands);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.brands.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:brands|max:255',
            'url' => 'required|url',
            'sort_order' => 'integer|between:1,99'
        ]);
        //return $this();
        Brand::create($request->all());
        return redirect(route('admin.brand.index'))->with('success', '添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $brand = Brand::find($id);
        //return $brand;
        return view('admin.shop.brands.edit')->with('brand', $brand);
    }

    /*
    public function update(Request $request,$id)
    {

    }
    */

    public function update(Request $request, $id)
    {
        //return $request->all();
        $this->validate($request, [
            'name' => 'required|max:255',
            'url' => 'url',
        ]);
        $brand = Brand::find($id);
        $brand->update($request->all());
        return redirect(route('admin.brand.index'))->with('success', '编辑品牌成功~');
    }

    public function destroy($id)
    {
        Brand::destroy($id);
        return redirect(route('admin.brand.index'))->with('error', '删除成功');
    }
    function sort_order(Request $request)
    {
        //return $request->sort_order;
        $brand = Brand::find($request->id);
        $brand->sort_order = $request->sort_order;
        $brand->save();
    }

    function is_something(Request $request)
    {
        $attr = $request->attr;
        $brand = Brand::find($request->id);
        $value = $brand->is_show ? false : true;
        $brand->$attr = $value;
        $brand->save();
    }



}
